﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Ivy.Common.Framework.Configuration
{
    /// <summary>
    /// This calss represents individual elements inside the main config section.
    /// There are main two type of elements (1) collection of elements (2) individual stand alone element.
    /// In this case it is a collection of emements, in this case it has to be inherited from 'ConfigurationElementCollection'
    /// Check the below example
    /// Example <FrameworkIvyConfigSection> is the main config section
    /// Example     <SmtpMailServers>       is the immediate child noide
    /// Example          <add name="MailServerOne" smtpHost="smtp.domain1.com" smtpPort="587" isHostSSLEnabled="true" smtpAuthUserId="useridOne" smtpAuthUserPassword="aaaaaa" isPasswordEncrypted="false" />
    /// Example          <add name="MailServerTwo" smtpHost="smtp.domain2.com" smtpPort="25" isHostSSLEnabled="false" smtpAuthUserId="useridTwo" smtpAuthUserPassword="??????" isPasswordEncrypted="true" />
    /// Example     </SmtpMailServers>
    /// Example </FrameworkIvyConfigSection>
    /// In this case this class represents <SmtpMailServers> node
    /// THIS IS CASE SENSETIVE AND SHOULD MATCH WITH THE CLASS NAME AND THE XML NODE NAME AS DECLARED IN THE MAIN CONFIG SECTION CLASS PROPERTY
    /// </summary>
    [ConfigurationCollection(typeof(SmtpServer))]
    public sealed class SmtpMailServers : ConfigurationElementCollection
    {
        #region Variables & Constants
        #endregion Variables & Constants

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Public Properties
        //public SmtpServer this[int index]
        //{
        //    get
        //    {
        //        return base.BaseGet(index) as SmtpServer;
        //    }
        //    set
        //    {
        //        if (base.BaseGet(index) != null)
        //        {
        //            base.BaseRemoveAt(index);
        //        }
        //        this.BaseAdd(index, value);
        //    }
        //}

        /// <summary>
        /// A property to return individual element from the collection list by index number
        /// in this case the element is an instance of 'SmtpServer' class.
        /// Check the below example
        /// Example <FrameworkIvyConfigSection> is the main config section
        /// Example     <SmtpMailServers>       is the immediate child noide
        /// Example          <add name="MailServerOne" smtpHost="smtp.domain1.com" smtpPort="587" isHostSSLEnabled="true" smtpAuthUserId="useridOne" smtpAuthUserPassword="aaaaaa" isPasswordEncrypted="false" />
        /// Example          <add name="MailServerTwo" smtpHost="smtp.domain2.com" smtpPort="25" isHostSSLEnabled="false" smtpAuthUserId="useridTwo" smtpAuthUserPassword="??????" isPasswordEncrypted="true" />
        /// Example     </SmtpMailServers>
        /// Example </FrameworkIvyConfigSection>
        /// In this case this property represents the inner most XML node which represents an instance 'SmtpServer' class.
        /// </summary>
        /// <param name="idx"></param>
        /// <returns></returns>
        public SmtpServer this[int idx]
        {

            get
            {

                return (SmtpServer)BaseGet(idx);

            }

        }


        /// <summary>
        /// A property to return individual element from the collection list by a unique string 'Name' tag 
        /// in this case the element is an instance of 'SmtpServer' class.
        /// Check the below example
        /// Example <FrameworkIvyConfigSection> is the main config section
        /// Example     <SmtpMailServers>       is the immediate child noide
        /// Example          <add name="MailServerOne" smtpHost="smtp.domain1.com" smtpPort="587" isHostSSLEnabled="true" smtpAuthUserId="useridOne" smtpAuthUserPassword="aaaaaa" isPasswordEncrypted="false" />
        /// Example          <add name="MailServerTwo" smtpHost="smtp.domain2.com" smtpPort="25" isHostSSLEnabled="false" smtpAuthUserId="useridTwo" smtpAuthUserPassword="??????" isPasswordEncrypted="true" />
        /// Example     </SmtpMailServers>
        /// Example </FrameworkIvyConfigSection>
        /// In this case this property represents the inner most XML node which represents an instance 'SmtpServer' class.
        /// The unique name tag in this case can be 'MailServerOne' or 'MailServerTwo'
        /// </summary>
        /// <param name="smtpServerName"></param>
        /// <returns></returns>
        public SmtpServer this[string smtpServerName]
        {
            get
            {
                return base.BaseGet(smtpServerName) as SmtpServer;
            }

        }
        #endregion Public Properties

        #region Public Methods
        #endregion Public Methods

        #region Events
        #endregion Events

        #region Private Methods
        protected override System.Configuration.ConfigurationElement CreateNewElement()
        {
            return new SmtpServer();
        }

        protected override object GetElementKey(System.Configuration.ConfigurationElement element)
        {
            //return ((SmtpServer)element).Name;
            return ((SmtpServer)(element)).Name;

        }
        #endregion Private Methods
    }
}
